package mh.demo.jdk.lang;

import mh.demo.PrintUtils;

public class ThreadDemo {

    private final static String A = "A";
    private final static String B = "B";

    public static void main(String[] args) {
        // 可以在main方法中测试出死锁效果，在junit中测试不出效果来。
        deadLockDemo(); // 死锁
    }

    /**
     * 死锁示例
     */
    public static void deadLockDemo() {
        Thread t1 = new Thread(() -> {
            synchronized (A) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (B) {
                    PrintUtils.println("1");
                }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (B) {
                synchronized (A) {
                    PrintUtils.println("2");
                }
            }
        });
        t1.start();
        t2.start();
    }

}
